package day13;

import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.Element;
import org.dom4j.io.SAXReader;

import java.io.File;
import java.util.List;

/**
 * @author oldliu
 * @since 1.0
 */
public class REadXML2 {
    //只能读
    public static void main(String[] args) throws Exception {
        //构建一个阅读器
        SAXReader reader = new SAXReader();
        //获得Document对象
        //
        //为什么不能src不能用
        //Document document = reader.read(new File("java高级/src/java82.xml"));

        //可以读，不能改！！！
        //屏蔽了开发时的环境信息
        Document document = reader.read(REadXML2.class.getClassLoader().getResourceAsStream("java82.xml"));
        Element root = document.getRootElement();
        System.out.println(root.getName());
        System.out.println("值：" + root.getTextTrim());
        //》》》List elements = root.elements();//所有的子节点
        //从当前的根节点开始找
        List<Element> list = root.selectNodes("book");
        for (Element e : list) {
            //访问e的属性,都是字符串。。。
            String item = e.attributeValue("item");
            int i = Integer.parseInt(item);
            //Element ename = (Element) e.selectNodes("name").get(0);
            Element ename = (Element) e.selectSingleNode("name");//查找唯一子节点
            String name = ename.getTextTrim();//!
            String time = ((Element) (e.selectSingleNode("time"))).getTextTrim();
            String desc = ((Element) (e.selectSingleNode("desc"))).getTextTrim();
            System.out.printf("这本书的信息是%d,name=%s,时间 %s,介绍 %s\n", i, name, time, desc);
        }
    }
}
